$(() => {
    const token = GetQueryString("token");
    let company_id = GetQueryString("company_id");
    let page = 1;
    let pageSize = 6;
    // 企业信息
    companyMsg();
    // 首次渲染商品页面
    company_goods();

    function companyMsg() {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'Company|company_detail', 'token': token, 'company_id': company_id },
            dataType: 'json',
            success: function(res) {
                // console.log(res);
                if (res.status == 1) {
                    let data = res.data;
                    let banner = '';

                    // 标题
                    $('title').html(data.company_name);

                    data.pic.forEach(item => {
                        banner += `
                    <div class="swiper-slide"><img src="${item.pic_url == '' ? '../img/banner.png' : item.pic_url}" alt="${item.pic_name}" data-url="${item.pic_url}" class="plusImg baimg" /></div>
                    `;
                    })
                    $('.swiper-wrapper').html(banner);
                    // banner轮播
                    new Swiper('.swiper-container', {
                        autoplay: 3000,
                        // pagination: '.swiper-pagination',
                        loop: true
                    })
                    $('.company_logo').attr("src", data.company_logo == '' ? '../img/orimg.png' : data.company_logo);
                    $('.company_name').html(data.company_name);
                    if (data.level_logo != '') {
                        $('.level_logo').attr('src', data.level_logo);
                    } else {
                        $('.level_logo').remove();
                    }

                    $('.read_count').html('浏览量' + data.read_count);
                    // 收藏
                    $('.select_company').attr('data-company_id', data.id);
                    if (data.has_follow == 1) {
                        $('#shopnati').addClass('on');
                    } else {
                        $('#shopnati').removeClass('on');
                    }
                }
            }
        })
    }

    // 商品
    function company_goods(page) {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'Company|company_goods', 'company_id': company_id, 'page': page, 'pagelimit': pageSize },
            dataType: 'json',
            success: function(res) {
                // console.log(res);
                if (res.status == 1) {
                    let data = res.data;
                    let html = '';
                    data.forEach(item => {
                        html += `
                        <li class="iz2item">
                            <a href="share_A_goodsDetail.html?id=${item.goods_id}&token=${token}" class="iz2link" >
                                <img src="${item.goods_thumb == '' ? '../img/banner.png' : item.goods_thumb}" alt="${item.goods_name}" class="iz2pic" />
                                <span class="iz2title">
                                    <p class="iz2ti1">${item.goods_name}</p>`
                                    if(item.price_show_model == 1){
                                        html +=`<p class="iz2ti2">¥${item.goods_price}</p>`
                                    }else if(item.price_show_model == 2){
                                        html +=`<p class="iz2ti2"><del style="color:#666;font-size:.24rem;">¥${item.price_before}</del>/<span style="font-size:.28rem;">¥${item.goods_price}</span></p>`
                                    }else{
                                        html +=`<p class="iz2ti2">¥${item.price_area}</p>`
                                    }
                                html +=`</span>
                            </a>
                        </li>
                        `;
                    })
                    if (data.length < pageSize) {
                        $('.notMore').css('display', 'flex');
                    }
                    $('.shopDetails').append(html);
                }
            }
        })
    }

    //相册
    function company_picture(page) {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'Company|company_goodpicture', 'company_id': company_id, 'page': page, 'pagelimit': pageSize },
            dataType: 'json',
            success: function(res) {
                // console.log(res);
                if (res.status == 1) {
                    let data = res.data;
                    let html = '';
                    data.forEach(item => {
                        html += `
                        <a href="javascript:;" class="shde_link">
                            <img src="${item.thumb_url == '' ? '../img/banner.png' : item.thumb_url}" alt="${item.pic_name}" data-url="${item.pic_url}" class="shde_pic plusImg" />${item.pic_name}
                        </a>
                        `;
                    })
                    if (data.length < pageSize) {
                        $('.notMore').css('display', 'flex');
                    }
                    $('.pictureDetails').append(html);
                }
            }
        })
    }

    //联系
    function company_contact() {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'Company|company_detail', 'company_id': company_id, 'token': token },
            dataType: 'json',
            success: function(res) {
                // console.log(res);
                if (res.status == 1) {
                    let data = res.data;
                    let html = '';
                    let phones = data.company_phones.split(',');
                    let addr = data.address.split(',');
                    addr.splice(0, 1, data.area_name + addr[0]);
                    html += `
                    <div class="shdetd">
                        <p class="shdetdt1">企业简介</p>
                        <p class="shdetdt2 hi" id="shdetdt2">${data.description}</p>
                        <a href="javascript:;" class="shdetdt3"><span class="shdetdt3link">更多</span><span class="shdetde3pic"></span></a>
                    </div>
                    <div class="shdetd">
                        <p class="shdetdt1">主营业务</p>
                        <p class="shdetdt2">${data.main_business}</p>
                    </div>
                    <div class="shdetd">
                        <p class="shdetdt1">联系电话</p>`
                    phones.forEach(item => {
                        html += `<p class="shdetdt2 myPhone">
                            手机：${item}
                        </p>`
                    })
                    html += `</div>
                    <div class="shdetd">
                        <p class="shdetdt1">联系地址</p>`
                    addr.forEach(ad => {
                        html += `
                            <p class="shdetdt2">
                                ${ad}
                            </p>`
                    })
                    html += `</div>
                    `;
                    $('.contactDetails').html(html);
                    let he = $('#shdetdt2').height();
                    if (he < 30) {
                        $('.shdetdt3').hide();
                    }
                }
            }
        })
    }

    // 建材圈
    function company_circle(page) {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'Company|company_forum', 'company_id': company_id, 'token': token, 'page': page, 'pagelimit': pageSize },
            dataType: 'json',
            success: function(res) {
                // console.log(res);
                if (res.status == 1) {
                    let data = res.data;
                    let html = '';

                    data.forEach(item => {
                        let date = getDate(item.create_time);
                        html += `
                        <div class="ringitem">
                            <div class="ringtop" style="display:flex;align-items:center;">
                                <img src="${item.head_logo == '' ? '../img/orimg.png' : item.head_logo}" alt="${item.name}" class="ringtpic" />
                                <div class="ringtco" style="flex:1;">
                                    <div class="ringtct1">
                                        ${item.name}
                                        <p class="ringtlr">${date}</p>
                                    </div>`
                        if (item.level_logo != '') {
                            html += `<img src="${item.level_logo}" class="userLevel" alt="${item.level_name}">`
                        }
                        html += `</div>
                            </div>
                            <p class="ringco">${item.content}</p>
                            <p class="slh">...</p>
                            <a href="javascript:;" class="ringco2">全文</a>
                            <div class="ringlilist" data-id="${item.id}">`
                        item.pictures.forEach(i => {
                            html += `<a href="javascript:;"><img src="${i.thumb_url == '' ? '../img/banner.png' : i.thumb_url}" alt="${i.pic_name}" data-url="${i.pic_url}" class="rilipic plusImg" /></a>`;
                        })
                        html += `</div>
                            <div class="browse">
                                <p class="brot">${item.read_count}人浏览</p>
                                <div class="brolr">
                                    <div class="brolrlink" deta-num="1">
                                        <span class="brolrpic brolrpic2"></span><span class="num">${item.digg_count}</span>
                                    </div>
                                </div>
                            </div>`
                        if (item.comments == '') {
                            html += '';
                        } else {
                            html += `<div class="brocomment">
                                <img src="../img/brotop.png" alt="" class="brotop" /><div class="commentLists">`

                            item.comments.forEach(p => {
                                html += `
                                <p class="brocoti"><a href="javascript:;" class="brocotlink">${p.nick_name}:</a>${p.content}</p>
                            `
                            })
                            html += `</div></div>`;
                        }
                        html += `</div>`;
                    })
                    if (data.length < pageSize) {
                        $('.notMore').css('display', 'flex');
                    }
                    $('.circleDetails').append(html);

                    // 文本溢出显示全文按钮
                    for (var i = 0; i < $(".ringco").length; i++) {
                        let height = $(".ringco").eq(i).height(); //div的宽度
                        let scrollHeight = $(".ringco").eq(i)[0].scrollHeight; //文本的宽度
                        if (scrollHeight > height) {
                            $('.ringco2').eq(i).show();
                            $('.slh').eq(i).show();
                        } else {
                            $('.ringco2').hide();
                            $('.slh').hide();
                        }
                    }
                    // 点击全文显示
                    $('.ringco2').on('click', function() {
                        $(this).parent().find(".ringco").css('height', 'auto');
                        $(this).parent().find('.slh').hide();
                        $(this).hide();
                    })
                } else if (res.status == 2) {
                    location.href = "fox://logout";
                } else {
                    alert(res.message);
                }
            }
        })
    }

    $('.goodsTab').on('click', function() {
        page = 1;
        $('.sh_delsitd').attr('data-type', 1);
        $('.shopDetails').html('');
        $('.notMore').css('display', 'none');
        company_goods(page);
    })
    $('.pictureTab').on('click', function() {
        page = 1;
        $('.sh_delsitd').attr('data-type', 2);
        $('.pictureDetails').html('');
        $('.notMore').css('display', 'none');
        company_picture(page);
    })
    $('.contactTab').on('click', function() {
        page = 1;
        $('.sh_delsitd').attr('data-type', 3);
        $('.notMore').css('display', 'none');
        company_contact();
    })
    $('.circleTab').on('click', function() {
        page = 1;
        $('.sh_delsitd').attr('data-type', 4);
        $('.circleDetails').html('');
        $('.notMore').css('display', 'none');
        company_circle(page);
    })

    // 分享下载链接及备案信息底部
    if ($('.downLoad').length) {
        $.ajax({
            type: "post",
            url: url,
            data: { "method": "Api|share_bottom" },
            dataType: 'json',
            success: function(res) {
                if (res.status == 1) {
                    let data = res.data;
                    let html = '';
                    data.download.forEach(item => {
                        html += `
                <li><a href="${item.url}">${item.type_name}</a></li>
                `;
                    })
                    $('.dlLists').html(html);
                    $('.icp_info').html(data.icp_info);
                    $('.other_msg').html(data.other_msg);
                }
            }
        })
    }

    // 下拉加载更多
    $(window).scroll(function() {
        if ($(document).scrollTop() >= $(document).height() - $(window).height() - 1) {
            // console.log('我到底了');
            if ($('.notMore').css('display') != "flex") {
                page++;
                let type = $('.sh_delsitd').attr('data-type');
                if (type == 1) {
                    company_goods(page);
                } else if (type == 2) {
                    company_picture(page);
                } else if (type == 4) {
                    company_circle(page);
                }
            }
        }
    })
})